#!/usr/bin/env python
# import sip
# sip.setapi('QVariant', 2)

from PySide.QtCore import *
from PySide.QtGui import *

import datahandler
import commonfunctions

class CreateSectionTab(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self, parent)

        self.data = datahandler.DataHandler()
        self.common = commonfunctions.CommonFunctions()
        self.parent = parent

        self.addSectionAction = QAction(QIcon(QDir.currentPath() + "/images/list-add.png"), "Add New Section", self)
        self.sectionUpAction = QAction(QIcon(QDir.currentPath() + "/images/go-up.png"), "Move Selected Section Up", self)
        self.addCaseSectionAction = QAction(QIcon(QDir.currentPath() + "/images/go-bottom.png"), "Add Selected Section to Case", self)
        self.delCaseSectionAction = QAction(QIcon(QDir.currentPath() + "/images/go-top.png"), "Remove Selected Section from Case", self)
        self.sectionDownAction = QAction(QIcon(QDir.currentPath() + "/images/go-down.png"), "Move Selected Section Down", self)
 
        self.sectiontb = QToolBar()
        self.sectiontb.addAction(self.addSectionAction)
        self.sectiontb.addSeparator()
        self.sectiontb.addAction(self.addCaseSectionAction)
        self.sectiontb.addAction(self.delCaseSectionAction)
        self.sectiontb.addAction(self.sectionDownAction)
        self.sectiontb.addAction(self.sectionUpAction)
        self.sectiontb.setStyleSheet("QToolBar { border: 0px }")

        for tmpaction in [self.sectionUpAction, self.sectionDownAction, self.addCaseSectionAction, self.delCaseSectionAction]:
            tmpaction.setEnabled(False)

        self.sectionList = QListWidget()
        self.gsectionList =QListWidget()
        self.gsectionLabel = QLabel("Available Sections")
        self.sectionLabel = QLabel("Ordered Case Notes Sections")
        for tmplist in [self.sectionList, self.gsectionList]:
            tmplist.setSelectionMode(QAbstractItemView.SingleSelection)
            tmplist.setAlternatingRowColors(True)
            tmplist.setFrameStyle(QFrame.Panel)

        # connect actions
        self.addCaseSectionAction.triggered.connect(self.addCaseSection)
        self.delCaseSectionAction.triggered.connect(self.delCaseSection)
        self.addSectionAction.triggered.connect(self.addSection)
        self.sectionDownAction.triggered.connect(self.sectionDown)
        self.sectionUpAction.triggered.connect(self.sectionUp)
        self.gsectionList.itemClicked.connect(self.updateActions)
        self.sectionList.itemClicked.connect(self.updateActions)

        self.sectionBox = QVBoxLayout()
        self.mainBox = QHBoxLayout()
        self.sectionBox.addWidget(self.sectiontb)
        self.sectionBox.addWidget(self.gsectionLabel)
        self.sectionBox.addWidget(self.gsectionList)
        self.sectionBox.addWidget(self.sectionLabel)
        self.sectionBox.addWidget(self.sectionList)
        self.mainBox.addLayout(self.sectionBox)
        self.setLayout(self.mainBox)

    def delCaseSection(self):
        currentsectionid = self.data.sql(self.data.getCurrentSectionId, [self.sectionList.currentItem().text()], 1)
        self.data.sql(self.data.removeCaseSection, [self.parent.currentcaseid, currentsectionid], 0)
        self.updateLists()

    def addSection(self):
        tmpsection, ok = QInputDialog.getText(self, "Add New Section", "Enter Section Title:")
        if ok and tmpsection:
            self.data.sql(self.data.insertSection, [tmpsection], 0)
        self.updateLists()

    def sectionDown(self):
        currentsectionid = self.data.sql(self.data.getCurrentSectionId, [self.sectionList.currentItem().text()], 1)
        self.data.sql(self.data.downCaseSection, [self.parent.currentcaseid, currentsectionid], 0)
        self.updateLists()

    def sectionUp(self):
        currentsectionid = self.data.sql(self.data.getCurrentSectionId, [self.sectionList.currentItem().text()], 1)
        self.data.sql(self.data.upCaseSection, [self.parent.currentcaseid, currentsectionid], 0)
        self.updateLists()
    
    def updateActions(self):
        if self.sender() is self.gsectionList:
            self.addCaseSectionAction.setEnabled(True)
            for tmpaction in [self.sectionUpAction, self.sectionDownAction, self.delCaseSectionAction]:
                tmpaction.setEnabled(False)
            if self.sectionList.currentItem() is not None:
                self.sectionList.currentItem().setSelected(False)
        elif self.sender() is self.sectionList:
            self.addCaseSectionAction.setEnabled(False)
            # determine if selected item is 1st or last in list, then don't enable up or down
            for tmpaction in [self.sectionUpAction, self.sectionDownAction, self.delCaseSectionAction]:
                tmpaction.setEnabled(True)
            if (self.sectionList.indexFromItem(self.sectionList.currentItem()).row() + 1) == self.sectionList.count():
                self.sectionDownAction.setEnabled(False)
            if self.sectionList.indexFromItem(self.sectionList.currentItem()).row() == 0:
                self.sectionUpAction.setEnabled(False)
            if self.gsectionList.currentItem() is not None:
                self.gsectionList.currentItem().setSelected(False)
        else:
            self.common.displayError("Invalid List", "Invalid List Action. If this error continues, please file a bug report.", "warning")

    def addCaseSection(self):
        currentsectionid = self.data.sql(self.data.getCurrentSectionId, [self.gsectionList.currentItem().text()], 1)
        self.data.sql(self.data.addCaseSection, [self.parent.currentcaseid, currentsectionid], 0)
        self.updateLists()

    def updateLists(self):
        self.updateSectionList()
        self.updateAvailableSectionList()

    def updateSectionList(self):
        self.sectionList.clear()
        self.sectionList.addItems(self.data.sql(self.data.updateSectionList, [self.parent.currentcaseid], 1))
        for tmpaction in [self.sectionUpAction, self.sectionDownAction, self.delCaseSectionAction]:
            tmpaction.setEnabled(False)

    def updateAvailableSectionList(self):
        self.gsectionList.clear()
        self.gsectionList.addItems(self.data.sql(self.data.updateAvailableSectionList, None, 1))
        self.addCaseSectionAction.setEnabled(False)
